﻿layui.use(['form', 'layedit', 'laydate'], function () {  
    var form = layui.form
    , layedit = layui.layedit
    , laydate = layui.laydate
	, layer = parent.layer === undefined ? layui.layer : parent.layer
	, laypage = layui.laypage,
		$ = layui.jquery;

  +
    //自定义验证规则
    form.verify({
        Names: function (value) {
            if (value.length <= 0) {
                return '部门名称不能为空';
            }
        }

    });

    function AddSubmit(data) {
        $.ajax({
            type: "post",
            url: "/Adddepartment/Section",
            data: data.field,
            dataType: "json",
            success: function (result) {
                if (result.State == 1)
                    layer.alert(result.Message, { icon: 1, time: 1.5 * 1000 });
                else
                    layer.msg(result.Message, { icon: 7, time: 1.5 * 1000 });
            }
        });
    };







    function Select() {
        var url = "/Adddepartment/Select";
        $.post(url, function (result) {
            var departments = JSON.parse(result.data);
            var deptSelect = $("#deptSelect");
            layui.each(departments, function (index, data) {
                var deptOption = $("<option value='" + data.D_Id + "'>" + data.D_Id + "</option>");
                deptSelect.append(deptOption);
            });
            form.render();
        });
    };


    
    $(function () {
        Select();
        //监听提交  subForm为lay-filter属性
        form.on('submit(demo2)', function (data) {
            AddSubmit(data);
            
            return false;
        });


        $(document).on("click", "#gogoto", function () {
       
        layer.open({
            type: 2,
            title: ['添加职位', 'background-color: #00bb9d;text-align:center;font-size:18px;color:#FFFFFF'],
            shadeClose: true,
            shade: [0.6, '#393D49'],
            maxmin: false,
            anim: 4,

            area: ['800px', '300px'],
            content: "/Adddepartment/PositionS",
            success: function (layero, index) {
                var body = layer.getChildFrame('body', index);//建立父窗体和子窗体关系                
                body.find('input[id="PLID"]').val(_this.attr("data-id"));//访问子窗体元素，
                //   注意会有延迟所以子窗体使用时要延迟加载
            },
            end: function () {
                //        如果是通过单击关闭按钮关闭弹出层，父画面没有此表单  
                if ($("#popupForm").length === 1) {
                    $("#popupForm").submit();
                }
                location.reload();   //页面刷新    --只要页面关闭 刷新父页面
            }
        });

        });




    });
 


});